﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.QLNH_BUS_WS;

namespace QLNH.MyForms
{
    public partial class AdminEventEdit : FormUC
    {
        #region Properties

        private bool _edit = false;
        private string _eventID = "";

        #endregion

        #region Constructor

        public AdminEventEdit(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_EVENT_EDIT;
        }

        public AdminEventEdit(LayoutUC mainLayout, object[] values)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_EVENT_EDIT;

            _edit = true;
            FillControlText(values);
        }

        #endregion

        #region Functions

        public override void Resize(Size size)
        {
            if (this.Size.Width < size.Width)
            {
                this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
            }
            else
            {
                this.Location = new Point(0, 0);
            }
        }

        //kiem tra du lieu va luu
        private void CheckAndSave(bool goBack)
        {
            if (this.tb_description.Text.Trim().Equals(""))
            {
                this.lb_warning.Visible = true;
                return;
            }

            EventDTO eventDTO = new EventDTO();
            eventDTO.ID = _eventID;
            eventDTO.Description = this.tb_description.Text.Trim();
            DateTime startDate = new DateTime(this.dtp_startDate.Value.Year, this.dtp_startDate.Value.Month, this.dtp_startDate.Value.Day,
                Int32.Parse(this.num_startHour.Value.ToString()), Int32.Parse(this.num_startMin.Value.ToString()), 0);
            DateTime endDate = new DateTime(this.dtp_endDate.Value.Year, this.dtp_endDate.Value.Month, this.dtp_endDate.Value.Day,
                Int32.Parse(this.num_endHour.Value.ToString()), Int32.Parse(this.num_endMin.Value.ToString()), 0);
            eventDTO.StartDate = startDate;
            eventDTO.EndDate = endDate;

            if (startDate > endDate)
            {
                this.lb_warning2.Visible = true;
                return;
            }

            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            if (_edit)
            {
                if (ws.EditEvent(eventDTO) == true)
                {
                    MessageBox.Show("Cập nhật sự kiện thành công");
                    if (goBack)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_EVENT_MANAGEMENT);
                    else
                    {
                        this.bt_saveAndBack.Enabled = false;
                        this.bt_save.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Cập nhật sự kiện thất bại");
                }
            }
            else
            {
                if (ws.AddEvent(eventDTO) == true)
                {
                    MessageBox.Show("Thêm sự kiện thành công");
                    if (goBack)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_EVENT_MANAGEMENT);
                    else
                    {
                        this.tb_description.Focus();
                        this.tb_description.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("Thêm sự kiện thất bại");
                }
            }
        }

        //dien du lieu vao cac control
        private void FillControlText(object[] values)
        {
            _eventID = values[0].ToString();
            string desc = values[1].ToString();
            DateTime start = DateTime.Parse(values[2].ToString());
            DateTime end = DateTime.Parse(values[3].ToString());

            this.tb_description.Text = desc;
            this.num_startHour.Value = start.Hour;
            this.num_startMin.Value = start.Minute;
            this.dtp_startDate.Value = start.Date;

            this.num_endHour.Value = end.Hour;
            this.num_endMin.Value = end.Minute;
            this.dtp_endDate.Value = end.Date;
        }

        #endregion

        #region Control Events

        private void AdminEventEdit_Load(object sender, EventArgs e)
        {
            this.bt_save.Enabled = this.bt_saveAndBack.Enabled = !_edit;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_EVENT_MANAGEMENT);
        }

        private void InfoChanged(object sender, EventArgs e)
        {
            if (this.lb_warning.Visible == true || this.lb_warning2.Visible == true)
            {
                this.lb_warning.Visible = false;
                this.lb_warning2.Visible = false;
            }

            if (_edit && this.bt_saveAndBack.Enabled == false && this.bt_save.Enabled == false)
            {
                this.bt_saveAndBack.Enabled = this.bt_save.Enabled = true;
            }
        }

        private void bt_saveAndBack_Click(object sender, EventArgs e)
        {
            CheckAndSave(true);
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            CheckAndSave(false);
        }

        #endregion
    }
}
